define(["bbcare", "jquery"], function ($bb, $) {
    var event = {}, vm = null, URL = $bb.baseUrl('followup')


    event.init = function () {
        if (vm) vm = null
        vm = createVue()
    }

    function createVue() {
        return new Vue({
            el: '#root',
            data: {
                search: {
                    name: '',
                    phone: '',
                    No: '',
                    BedNo: '',
                    roomNo: '',
                    wardNo: '',
                    loading: false
                },
                table: {
                    data: [],
                    loading: false,
                    total: 0,
                    current: 1,
                    size: 10,
                },
                dialog: {
                    show: false,
                    loading: false,
                    form: {
                        remark: '',
                        milkQuantity: '',
                        milkMsg: ''
                    }
                },
                currentPatient: null,
                remark: [],
                otherRemark: ''
            },
            watch: {
                remark: function (val) {
                    this.dialog.form.remark = val.join(',')
                }
            },
            created: function () {
                this.getTableList();
                // 初始化通知内容
                var tDate = this.getTomorrowDate(1);
                this.dialog.form.milkMsg =  tDate + '所需母乳量为 x ml，请在（' + tDate + ' / 08:00~09:00）送至母乳接收处。';
            },
            methods: {
                getTomorrowDate: function (after) {
                    var date = new Date().getTime() + 3600 * 24 * 1000 * after;
                    date = new Date(date);
                    var year = date.getFullYear(),
                        month = date.getMonth() + 1,
                        day = date.getDate();
                    var dateString = [year, month, day].map(function (item) {
                        return item < 10 ? '0' + item : item;
                    })
                    return dateString.join('-');
                },
                rowStyle: function (row) {
                    if (row.row.milkPrepareCount > 0) {
                        return {
                            'background-color': '#e8e8e8'
                        }
                    }
                },
                handleSearch: function () {
                    this.search.loading = true
                    this.getTableList()
                },
                /**
                 * 改变当前页大小
                 * @param {*} num 
                 */
                handleSizeChange: function (num) {
                    this.table.size = num;
                    this.getTableList()
                },
                handleShowDialog: function (row) {
                    this.dialog.show = true
                    this.currentPatient = row
                    this.dialog.name = row.name
                    this.dialog.hospitalNo = row.inPatientNo
                    this.dialog.bedNo = row.hospitalBedNo
                },
                handleCancel: function () {
                    this.dialog.show = false
                    this.remark = []
                    this.dialog.form.remark = []
                    this.dialog.form.milkQuantity = ''
                    this.currentPatient = null
                },
                handleSubmit: function () {
                    if (!this.dialog.form.milkMsg) return this.$message.error('请填写通知内容')
                    this.sendData();
                },
                sendData: function () {
                    this.dialog.loading = true
                    var that = this
                    var remark = this.dialog.form.remark.indexOf('其它') > -1 ?
                        this.dialog.form.remark.replace('其它', this.otherRemark) :
                        this.dialog.form.remark

                    var params = {
                        userId: this.currentPatient.id,
                        userName: this.currentPatient.name,
                        gender: this.currentPatient.gender,
                        birthday: this.currentPatient.birthday,
                        phone: this.currentPatient.phone,
                        inPatientNo: this.currentPatient.inPatientNo,
                        hospitalBedNo: this.currentPatient.hospitalBedNo,
                        hospitalName: this.currentPatient.hospitalName || sessionStorage.getItem('hosipitalName'),
                        hospitalRoomNo: this.currentPatient.hospitalRoomNo,
                        hospitalWard: this.currentPatient.hospitalWard,
                        tenantId: this.currentPatient.tenantId,
                        accountId: this.currentPatient.accountId,
                        milkQuantity: this.dialog.form.milkQuantity,
                        milkMsg: this.dialog.form.milkMsg,
                        remark: remark
                    }

                    $bb.ajax({
                        url: URL + 'followup/milkPreparationRecord/setMilkQuantity',
                        data: JSON.stringify(params),
                        success: function (res) {
                            if (res.success) {
                                that.dialog.show = false
                                that.dialog.form.remark = []
                                that.remark = []
                                that.dialog.form.milkQuantity = ''
                                that.$message.success('提交成功')
                                that.getTableList()
                            } else {
                                that.$message.error(res.msg)
                            }
                            that.dialog.loading = false
                        }
                    })
                },
                /**
                 * 改变页码
                 */
                handleChangePage: function (num) {
                    this.table.current = num
                    this.getTableList()
                },
                /**
                 * 获取列表数据
                 */
                getTableList: function () {
                    this.table.loading = true
                    var that = this
                    var params = {
                        currentPage: this.table.current,
                        pageSize: this.table.size,
                        userName: this.search.name,
                        phone: this.search.phone,
                        inPatientNo: this.search.No,
                        hospitalBedNo: this.search.BedNo,
                        type: 1,
                        hospitalRoomNo: this.search.roomNo,
                        hospitalWard: this.search.wardNo,
                    }
                    $bb.ajax({
                        url: URL + 'followup/milkPreparationRecord/milkPrepPatientPage',
                        data: JSON.stringify(params),
                        success: function (res) {
                            if (res.success) {
                                res.data.data.forEach(function (item, index) {
                                    item.No = (that.table.current - 1) * that.table.size + index + 1
                                    item.checked = false
                                })
                                that.table.data = res.data.data
                                that.table.total = res.data.count
                            } else {
                                that.$message.error('请求列表数据出错')
                            }
                            that.table.loading = false
                            that.search.loading = false
                        }
                    })
                },
               

            }
        })
    }

    return event
})